package se.lth.student.axisandroidcam.client;

import se.lth.cs.fakecamera.Axis211A;
import se.lth.student.axisandroidcam.client.cameraclients.ClientIn;
import se.lth.student.axisandroidcam.client.cameraclients.ClientOut;
import se.lth.student.axisandroidcam.client.cameraclients.NetworkClientIn;
import se.lth.student.axisandroidcam.client.cameraclients.NetworkClientOut;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;

public class AxisAndroidCamera extends Activity {

	private static CameraAggregator imgAdapter;
	private GridView gridView;
	private Axis211A fakeCamera;
    private static final int MENU_MOVIE_MODE = 0;
    private static final int MENU_IDLE_MODE = 1;
    private static final int MENU_FORCE_SYNCH = 2;
	public static int PORT;
	private boolean isSynch = false;
	private ClientIn clientIn;
	private NetworkClientOut clientOut;
	private ClientIn clientIn2;
	private NetworkClientOut clientOut2;
	private SynchronizeThread synchThread;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		imgAdapter = new CameraAggregator(this);
		Toolbox.printDebug("onCreate()");
		gridView = (GridView) findViewById(R.id.gridview);
		gridView.setAdapter(imgAdapter);
		PORT = getResources().getInteger(R.integer.PORT);
		
		synchThread = new SynchronizeThread(this, imgAdapter);
		synchThread.start();
		/*
		 * temp-booting up server
		 */
		/*
		String[] args = new String[1];
		args[0] = "1";
		Server.main(args);
		Toolbox.printDebug("Done!");
		
		args = new String[1];
		args[0] = "2";
		Server.main(args);
		Toolbox.printDebug("Done!");
		*/

		
	/*	final Camera cam1 = new Camera(1);
		cam1.associateWithObserver(imgAdapter);
		final Camera cam2 = new Camera(2);
		cam2.associateWithObserver(imgAdapter);
*/
//		cameraSynch = new SynchronizeThread(this, 2, 50, 500);
//		cameraSynch.addCamera(cam1);
//		cameraSynch.addCamera(cam2);
//		cameraSynch.start();

		
		clientIn = new NetworkClientIn(1, this, "192.168.1.103", synchThread);
		clientOut = new NetworkClientOut(1, this, "192.168.1.103");
		clientIn.start();
		clientOut.start();
		
		clientIn2 = new NetworkClientIn(2, this, "192.168.1.103", synchThread);
		clientOut2 = new NetworkClientOut(2, this, "192.168.1.103");
		clientIn2.start();
		clientOut2.start();
	}

    /* Creates the menu items */
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_MOVIE_MODE, 0, "Movie Mode");
        menu.add(0, MENU_IDLE_MODE, 0, "Idle Mode");
        menu.add(0, MENU_FORCE_SYNCH, 0, "Toggle Synch");
        return true;
    }


    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        	case MENU_MOVIE_MODE:
        		clientOut.setMovieMode(1);
        		clientOut2.setMovieMode(1);
            return true;
        	case MENU_IDLE_MODE:
        		clientOut.setMovieMode(0);
        		clientOut2.setMovieMode(0);
            return true;
        	case MENU_FORCE_SYNCH:
        		isSynch = !isSynch;
        		synchThread.setSynch(isSynch);
            return true;
        }
        return false;
    }
	
    @Override
    protected void onPause() {
            super.onPause();
            Toolbox.printDebug("onPause()");
    }
 
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            
            Toolbox.printDebug("onConfigurationChanged()");
    }

    @Override
    protected void onResume() {     
            super.onResume();
            Toolbox.printDebug("onResume()");
    }
    
    @Override
    protected void onRestart() {    
            super.onRestart();
            Toolbox.printDebug("onRestart()");
    }

    
    @Override
    public void onLowMemory(){
            super.onLowMemory();
            Toolbox.printDebug("onLowMemory()");
    }
    
    @Override
    protected void onStop(){
            super.onStop();
            Toolbox.printDebug("onStop()");
    }
    
    @Override
    protected void onDestroy(){
            super.onStop();
            Toolbox.printDebug("onDestroy()");
            System.exit(0);
            super.finish();
    }

}
